﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using DIWAGIS;
using DIWAGIS.ApplicationLayer.XemThongTin;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;

namespace DIWAGIS.ApplicationLayer.Commands
{
    /// <summary>
    /// Command that works in ArcMap/Map/PageLayout
    /// </summary>
    [Guid("fc5c0785-2030-4af9-b31a-1943dea1e16d")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("DIWAGIS.ApplicationLayer.Commands.CmdUpdateInfo")]
    public sealed class cmdUpdateInfo : BaseCommand
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Register(regKey);
            ControlsCommands.Register(regKey);
        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Unregister(regKey);
            ControlsCommands.Unregister(regKey);
        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper = null;
        private IMapControl4 m_map;
        private ILayer m_targetLayer;
        public ILayer targetLayer
        {
            get { return m_targetLayer; }
            set { m_targetLayer = value; }
        }

        private IEngineEditor m_editor;
        public IEngineEditor editor
        {
            get { return m_editor; }
            set { m_editor = value; }
        }

        private IWin32Window m_owner;
        public IWin32Window owner
        {
            set { m_owner = value; }
            get { return m_owner; }
        }

        public cmdUpdateInfo()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = "Cập nhật thông tin đối tượng"; //localizable text
            base.m_caption = "Cập nhật thông tin đối tượng";  //localizable text 
            base.m_message = "Cập nhật thông tin đối tượng";  //localizable text
            base.m_toolTip = "Cập nhật thông tin đối tượng";  //localizable text
            base.m_name = "Cập nhật thông tin đối tượng";   //unique id, non-localizable (e.g. "MyCategory_MyCommand")

            try
            {
                //
                // TODO: change bitmap name if necessary
                //
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        #region Overridden Class Methods

        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
                return;

            try
            {
                m_hookHelper = new HookHelperClass();
                m_hookHelper.Hook = hook;
                if (m_hookHelper.ActiveView == null)
                    m_hookHelper = null;
            }
            catch
            {
                m_hookHelper = null;
            }

            if (m_hookHelper == null)
                base.m_enabled = false;
            else
                base.m_enabled = true;

            // TODO:  Add other initialization code
            if (m_hookHelper.Hook is IMapControl4)
            {
                m_map = (IMapControl4)m_hookHelper.Hook;
            }
            else if (m_hookHelper.Hook is IToolbarControl2)
            {
                IToolbarControl2 _toolBar;
                _toolBar = (IToolbarControl2)m_hookHelper.Hook;
                m_map = (IMapControl4)_toolBar.Buddy;
            }
        }

        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            // TODO: Add CmdUpdateInfo.OnClick implementation
            frmCapNhatThongTinDoiTuong frmCapNhatThongTin;
            bool isTTopen = false;
            foreach (var item in Application.OpenForms)
            {
                if (item is frmCapNhatThongTinDoiTuong)
                {
                    frmCapNhatThongTin = ((frmCapNhatThongTinDoiTuong)item);
                    //frmCapNhatThongTin.selectedFeature = SelectedFeature;
                    frmCapNhatThongTin.targetLayer = m_targetLayer;
                    frmCapNhatThongTin.mapControl = m_map;
                    frmCapNhatThongTin.editor = m_editor;
                    frmCapNhatThongTin.LoadData();
                    frmCapNhatThongTin.Activate();
                    isTTopen = true;
                }
            }
            if (isTTopen == false)
            {
                frmCapNhatThongTin = new frmCapNhatThongTinDoiTuong();//m_map
                //frmCapNhatThongTin.selectedFeature = SelectedFeature;
                frmCapNhatThongTin.targetLayer = m_targetLayer; ;
                frmCapNhatThongTin.mapControl = m_map;
                frmCapNhatThongTin.editor = m_editor;
                frmCapNhatThongTin.LoadData();
                frmCapNhatThongTin.Show(m_owner);
            }
        }

        public override bool Enabled
        {
            get
            {
                //check whether Editing 
                if (m_editor != null)
                {
                    if (m_editor.EditState == esriEngineEditState.esriEngineStateNotEditing)
                    {
                        return false;
                    }
                }
                //conditions have been met so enable the tool
                return true;
            }
        }

        #endregion
    }
}

